# coding:utf-8
'''
Created on 2019年3月11日

用于新增需求的业务处理

@author: L.Wen
'''
from django.shortcuts import render, get_object_or_404, HttpResponseRedirect
from django.contrib.auth.decorators import login_required
from django.views.decorators.csrf import csrf_protect
from .. import tasks, models, forms
import json
from django.contrib.auth.models import User
from django.http import JsonResponse

'''
此方法用于自定义端口的扫描
'''


@login_required
@csrf_protect
def task_action(request):
    user = request.user
    error = ''
    asset_ip = request.POST.get('ip')
    asset_port = request.POST.get('port')

    asset_ip_list = json.loads(asset_ip)
    asset_port_list = json.loads(asset_port)

    if len(asset_ip_list) == 0:
        error = '未选择符合要求资产'
    else:
        action = request.POST.get('action')
        if action == 'port':
            tasks.asset_port.delay(user.id, asset_ip_list, asset_port_list)
            # tasks.asset_port(user.id,asset_id_list)
            error = '任务已提交'
        else:
            error = '参数错误'
    return JsonResponse({'error': error})
